#include "E_Pthread_Pool.h"
#include "iostream"
#include "string"
#include "TCP.h"
#include "appData.h"

using namespace std;

class my_tast : public Task{
public:
	std::string str;
	my_tast(std::string st){
	this->str = st;	
	}
	int run(){
	if(str[0] == '1')
	cout<<"注册新用户:"<<&str[1]<<endl;
       else
	 cout<<"Recvive："<<str<<endl;
	}
};
//------------------------------

//--------------TCP-test---------------
class TCP_tast : public Task{//线程池处理用户连接任务
public:
	string id1,id2,id3;
	int *connfd = nullptr;
	TCP_tast (int *ptr){
	connfd = ptr;	
	}
	int run(){
	char buf[100];
	int ret;
         while(1){

	memset(buf, 0, sizeof(buf));
	ret = read(*connfd, buf, sizeof(buf));
	if(ret < 0 || ret == 0){
		puts("read eoor");
		break;
	}
	buf[ret-1] = 0;//不含\n

   if(buf[0] == '1'){//注册请求
		isAlreadRegister(buf, connfd);
   }
   
   else if(buf[0] == '2'){//登录请求
		loginIsSuccess(buf, connfd);
   }
   else if(buf[0] == '3'){//更新设备状态
	  	updateEquipState(buf, connfd);
   }
   else if(buf[0] == '4'){//更新设备状态
	  	updateEquipState(buf, connfd);
   }
   else if(buf[0] == '5'){//添加设备
	    addEquip(buf, connfd);//添加设备
   }
   else if(buf[0] == '6'){//删除设备
	    deleteEquip(buf, connfd);
   }
   else if(buf[0] == '7'){//读取设备互动数据库-将开锁记录发送至APP
		cout<<"读取设备互动数据库"<<endl;
	    sendDataBase(buf, connfd);
   }
   //设备发送的信息
	else if(buf[0] == char(0x0a)){//开锁信息
		cout<<"设备发来信息"<<endl;
		equipReceiveDate(buf, connfd);
	}
	else if(buf[0] == '8'){//远程开锁-远程授权
		cout<<"APP控制指令:"<<buf<<endl;
		appControl(buf, connfd);
	}
   else{
	   
   }
   
}
	close(*connfd);
	free(connfd);
	cout<<"APP TCP end"<<endl;;
//	close(sockfd);

		}
	};


void* startReceiveTCP(void*);

E_PThread_Pool thread_pool(10,2,200);

int main(){

	pthread_t id;
	pthread_create(&id, NULL, startReceiveTCP ,NULL);//启动TCP服务器


	while(1);

return 0;
}

void* startReceiveTCP(void*){//TCP服务器
//-------------socker()------------------
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0){
	printf("sockfd is error\n");
	return nullptr;
}else
	printf("sock....、\n");
//端口复用 快速重启服务器
int opt = 1;
setsockopt(sockfd,SOL_SOCKET, SO_REUSEADDR,&opt, sizeof(opt));


struct sockaddr_in myaddr;
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(75);
myaddr.sin_addr.s_addr = INADDR_ANY;
//client struct sockaddr_in myaddr;
struct sockaddr_in client_addr;
client_addr.sin_family = AF_INET;
int client_addr_size = sizeof(client_addr);

//--------------bind()----------------------
if(0>bind(sockfd, (struct sockaddr*)&myaddr, sizeof(myaddr))){
	printf("bind error..\n");
	return nullptr;
}
else
	printf("bind..........\n");
//-------------listen()--------------
if(0 > listen(sockfd, 8)){
	perror("listen error...\n");
	return nullptr;;
}
else{
	printf("listen.....\n");
}
//-------------accept---------------
while(1){
	int* connfd = (int*)malloc(sizeof(int));
	*connfd = accept(sockfd, (struct sockaddr*)&client_addr, (socklen_t*)&client_addr_size);
	if(connfd < 0){
		puts("accept error");
		return nullptr;;
	}
	else{
		//输出客户端地址
		printf("accept....addr:%s : %d\n",inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));	
		
		TCP_tast* tcp_tast = new TCP_tast(connfd);
		thread_pool.addTask(tcp_tast);
      		}
	}

}  
